<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>报名人管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
		$(document).ready(function() {
			//$("#name").focus();
			$("#inputForm").validate({
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
		});
		
		function checkPassword()
		{
			var password = $("#password").val();
			var passwordCfm = $("#passwordCfm").val();
			
			if(password == passwordCfm)
			{
				$("#passwordCfmTip").hide();
			}
			
		}
		
		function doSubmit()
		{
			
			var userId = "${emsUser.id}";
			
			var loginName = $("#loginName").val();
			var mobile = $("#mobile").val();
			var name = $("#name").val();
			var password = $("#password").val();
			var passwordCfm = $("#passwordCfm").val();
			
			var isBlank = !loginName || !mobile || !name;
			// 新增时，添加密码判断（修改时，不能修改密码）
			if(userId == '')
			{
				isBlank = isBlank || !password || !passwordCfm
			}
			// 若姓名、密码或者手机号为空，则留给validate去校验
			if(isBlank)
			{
				return true;
			}
			
			var result = false;
			if(!REG_LOGINNAME.test(loginName)){
				$("#loginNameTip").html("输入的登录名不符合要求，请重新输入");
		    	$("#loginNameTip").show();
		    	return false;
			}
			else
			{
				$("#loginNameTip").hide();
				result = true;
			}
			
			// 新增时，判断密码
			if(userId == '')
			{
				if(password.length < 6)
				{
					$("#passwordTip").html("密码长度不符合要求");
			    	$("#passwordTip").show();
			    	return false;
				}
				else
				{
					$("#passwordTip").hide();
					result = true;
				}
				
				if(password != passwordCfm)
				{
					$("#passwordCfmTip").html("输入的密码和确认密码不一致");
			    	$("#passwordCfmTip").show();
			    	return false;
				}
				else
				{
					$("#passwordCfmTip").hide();
					result = true;
				}
			}
			
			
			if(!REG_MOBILE_PATTERN.test(mobile)){
				$("#mobileTip").html("输入的手机号码格式错误，请重新输入");
		    	$("#mobileTip").show();
		    	return false;
			}
			else
			{
				$("#mobileTip").hide();
				result = true;
			}
			
			// 若姓名或者手机号为空，则留给validate去校验
			$.ajax({
			       type: "POST",
			       url: ctx+"/ems/emsUser/checkExists",
			       dataType: "text",
			       data:"loginName="+loginName + "&mobile=" + mobile,
				   async: false,
			       success: function(jsonMajor){
				       // 转换为json对象
				       jsonMajor = eval('(' + jsonMajor + ')');
				       
				       var isLoginName = jsonMajor['loginName'];
				       var isMobile = jsonMajor['mobile'];
				       
				       
				       if("yes" == isLoginName)
			    	   {
				    	   // $("#messageBox").text("手机号为'" + mobile + "'且名称含'" + name + "'的学员已经存在。");
				    	   // $("#messageBox").show();
				    	   
				    	   $("#loginNameTip").html("登陆名" + loginName + "已经存在。");
				    	   $("#loginNameTip").show();
				    	   result = false;
			    	   }
				       else
				       {
				    	   $("#loginNameTip").hide();
				       }
				       
				       if("yes" == isMobile)
			    	   {
				    	   // $("#messageBox").text("手机号为'" + mobile + "'且名称含'" + name + "'的学员已经存在。");
				    	   // $("#messageBox").show();
				    	   
				    	   $("#mobileTip").html("手机号码" + mobile + "已经存在。");
				    	   $("#mobileTip").show();
				    	   result = false;
			    	   }
				       else
				       {
				    	   $("#mobileTip").hide();
				       }
				       //$("#messageBox").text("输入有误，请先更正。");
					   
			       }
			    });
			
			
			return result;
		}
	</script>
</head>
<body>
	<ul class="nav nav-tabs">
		<li><a href="${ctx}/ems/emsUser/">报名人列表</a></li>
		<li class="active"><a href="${ctx}/ems/emsUser/form?id=${emsUser.id}">报名人<shiro:hasPermission name="ems:emsUser:edit">${not empty emsUser.id?'修改':'添加'}</shiro:hasPermission><shiro:lacksPermission name="ems:emsUser:edit">查看</shiro:lacksPermission></a></li>
	</ul><br/>
	<form:form id="inputForm" modelAttribute="emsUser" action="${ctx}/ems/emsUser/save" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<sys:message content="${message}"/>
		<div class="control-group">
			<label class="control-label">登录名：</label>
			<div class="controls">
				<form:input path="loginName" id="loginName" onchange="doSubmit();" htmlEscape="false" maxlength="64" class="input-xlarge required"/>
				<label id="loginNameTip" class='error' style="display: none;"></label>
				<span class="help-inline"><font color="red">*</font> </span>
				<span class="help-inline"><font>全站唯一 (字母开头，可包含数字或"_")</font> </span>
			</div>
		</div>
		
		<c:if test="${empty emsUser.id}">
			<div class="control-group">
				<label class="control-label">密码：</label>
				<div class="controls">
					<form:password path="password" id="password" htmlEscape="false" maxlength="64" class="input-xlarge required"/>
					<label id="passwordTip" class='error' style="display: none;"></label>
					<span class="help-inline"><font color="red">*</font> </span>
					<span class="help-inline"><font>密码长度至少6位</font> </span>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">密码确认：</label>
				<div class="controls">
					<form:password path="passwordCfm" id="passwordCfm" onchange="checkPassword();" htmlEscape="false" maxlength="64" class="input-xlarge required"/>
					<label id="passwordCfmTip" class='error' style="display: none;"></label>
					<span class="help-inline"><font color="red">*</font> </span>
				</div>
			</div>
		</c:if>
		<div class="control-group">
			<label class="control-label">姓名：</label>
			<div class="controls">
				<form:input path="name" id="name" htmlEscape="false" maxlength="64" class="input-xlarge required"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">手机号码：</label>
			<div class="controls">
				<form:input path="mobile" id="mobile" htmlEscape="false" maxlength="16" class="input-xlarge required"/>
				<label id="mobileTip" class='error' style="display: none;"></label>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">电子邮箱：</label>
			<div class="controls">
				<form:input path="email" htmlEscape="false" maxlength="64" class="input-xlarge "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">地址：</label>
			<div class="controls">
				<form:input path="address" htmlEscape="false" maxlength="256" class="input-xlarge "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">是否启用：</label>
			<div class="controls">
				<form:select path="status" class="input-xlarge ">
					<form:option value="1" label="是"/>
					<form:options items="${fns:getDictList('yes_no')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
				</form:select>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">备注：</label>
			<div class="controls">
				<form:textarea path="remarks" htmlEscape="false" rows="4" maxlength="255" class="input-xxlarge "/>
			</div>
		</div>
		<%-- <div class="control-group">
			<label class="control-label">购物车：</label>
			<div class="controls">
				<form:input path="cartId" htmlEscape="false" maxlength="64" class="input-xlarge "/>
			</div>
		</div> --%>
		<div class="form-actions">
			<shiro:hasPermission name="ems:emsUser:edit"><input id="btnSubmit" class="btn btn-primary" type="submit" onclick="return doSubmit();" value="保 存"/>&nbsp;</shiro:hasPermission>
			<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
		</div>
	</form:form>
</body>
</html>